package com.jsbj.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 使用 Feign进行远程调用时，先经过此拦截器，在此拦截器中将请求头带上访问令牌
 */
@Component
public class FeignRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 通过RequestContextHolder工具来获取请求相关变量
        ServletRequestAttributes attributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(attributes != null) {
            // 获取请求对象
            HttpServletRequest request = attributes.getRequest();

            //feign远程调用时增加请求头
            //需要发送的请求头
            String[] headerNames = new String[]{ HttpHeaders.AUTHORIZATION, "tenantCode", "userId"};

            for (String headerName : headerNames) {
                String value = request.getHeader(headerName);
                if(StringUtils.isNotEmpty(value)) {
                    // 在使用feign远程调用时，增加请求头
                    requestTemplate.header(headerName, value);
                }
            }
        }
    }
}
